package com.jswlw.controller.api.wrapper;

import com.jswlw.common.BaseException;
import com.jswlw.common.ResponseStatus;
import com.jswlw.common.utils.SnUtils;
import com.jswlw.controller.api.helper.UserHelper;
import com.jswlw.controller.api.req.order.SaveOrderRequest;
import com.jswlw.controller.api.resp.OrderListResponse;
import com.jswlw.controller.api.resp.SaveOrderResponse;
import com.jswlw.entity.biz.BillSet;
import com.jswlw.entity.biz.DevInfo;
import com.jswlw.entity.biz.OrderInfo;
import com.jswlw.service.biz.BillSetService;
import com.jswlw.service.biz.DevInfoService;
import com.jswlw.service.biz.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Component
public class OrderWrapper {

    @Autowired
    OrderInfoService orderInfoService;
    @Autowired
    UserHelper userHelper;
    @Autowired
    DevInfoService devInfoService;
    @Autowired
    BillSetService billSetService;

    public OrderListResponse orderList(String devId, Integer status) {
        OrderListResponse response = new OrderListResponse();
        OrderInfo filter = new OrderInfo();
        filter.setDevId(devId);
        if (Objects.nonNull(status) && status.intValue() != -1) {
            filter.setPayStatus(status);
        }
        filter.setUserId(userHelper.findUser().getId());
        List<OrderInfo> list = orderInfoService.queryList(filter);
        List<OrderListResponse.OrderListData> data = new ArrayList<>();
        list.forEach(orderInfo -> {
            data.add(OrderListResponse.OrderListData.builder()
                    .id(orderInfo.getId())
                    .amount(orderInfo.getAmount())
                    .createTime(orderInfo.getCreateTime())
                    .payStatus(orderInfo.getPayStatus())
                    .build());
        });
        response.setData(data);
        response.setCode(ResponseStatus.SUCCESS.getCode());
        response.setMsg(ResponseStatus.SUCCESS.getMsg());
        return response;
    }

    public SaveOrderResponse saveOrder(SaveOrderRequest request) {
        SaveOrderResponse response = new SaveOrderResponse();
        String sn = SnUtils.getOrderNo();
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setBillSetId(request.getBillSetId());
        orderInfo.setAmount(request.getAmount());
        orderInfo.setProductName(request.getBillSetName() + request.getBillSetId());
        orderInfo.setSn(sn);
        orderInfo.setCreateTime(new Date());
        orderInfo.setPayType(1);
        orderInfo.setDevId(request.getDevId());
        orderInfo.setPayStatus(0);

        BillSet billSet = billSetService.find4Id(request.getBillSetId());
        DevInfo devInfo = devInfoService.find4Id(request.getDevId());
        if (!devInfo.getOnlineStatus().equals(1)) {
            BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "设备不在线");
        }
        devInfo.setBillTypeId(billSet.getBillingModel());
        orderInfo.setUserId(devInfo.getUserId());
        orderInfo.setDistributorId(devInfo.getDistributorId());
        orderInfo.setDistributorName(devInfo.getDistributorName());

        orderInfo.setTel(devInfo.getTel());
        orderInfoService.add(orderInfo);

        response.setData(SaveOrderResponse.OrderSn.builder().sn(sn).build());
        response.success();
        return response;
    }
}
